const matrixData = (
  column?: any,
  lineData?: any,
  tableData?: any,
  showType?: 1 | 2
) => {
  const columns: any = [
    {
      title: '职位层级（职等）',
      dataIndex: 'grade',
      fixed: 'left',
      width: 100,
      align: 'center',
      cellStyle: { border: ' 1px solid #e4e4e4' }
    },
    {
      title: '职级',
      dataIndex: 'rank',
      fixed: 'left',
      width: 100,
      align: 'center',
      cellStyle: { border: ' 1px solid #e4e4e4' }
    }
  ];
  const data: any = [];
  const codeAll: string[] = [];
  const getSortData = async () => {
    try {
      column?.forEach((ele: any) => {
        columns.push(changeColumns(ele, true));
      });
      lineData?.content?.forEach((ele: any) => {
        changeData(ele);
      });
      mergeTableData(tableData);
      data.sort((a: any, b: any) => {
        return b.rank - a.rank;
      });
    } catch (error) {
      console.log(error);
    }
  };
  const changeColumns = (data: any, type?: boolean) => {
    const newData: any = {
      title: '',
      align: 'center',
      width: 140,
      cellStyle: { border: ' 1px solid #e4e4e4' },
      color: data.color,
      children: []
    };
    if (type) {
      newData.title = data.name + data.code;
      showType === 1
        ? newData.children.push({
            title: '职级代号',
            align: 'center',
            width: 140,
            dataIndex: data.name,
            cellStyle: { border: ' 1px solid #e4e4e4' }
          })
        : null;
      codeAll.push(data.name);
    } else {
      newData.title = data.name;
    }

    if (data?.children) {
      data?.children?.forEach((ele: any) => {
        newData.children.push(changeColumns(ele));
      });
    } else {
      newData.dataIndex = data.name;
      newData.slotName = 'customize';
      newData.children = null;
    }
    newData.bodyCellStyle = (record: any) => {
      if (showTableData(record, newData) && data.color) {
        return { backgroundColor: `${data.color}` };
      } else if (showTableData(record, newData) && !data.color) {
        return { backgroundColor: '#9cdcfe' };
      } else {
        return { backgroundColor: '#fff' };
      }
    };
    return newData;
  };
  const changeData = (datas: any) => {
    for (let index = datas.levelDown; index <= datas.levelUp; index++) {
      const codeObj: any = {};
      codeAll?.forEach((ele) => [(codeObj[ele] = ele + index)]);
      data.push({
        grade: datas.name,
        rank: index,
        code: 1,
        ...codeObj
      });
    }
  };
  const mergeTableData = (tableData: any) => {
    data?.forEach((ele: any) => {
      tableData?.forEach((item: any) => {
        if (item.level === ele.rank) {
          ele[item.dataIndex] = { value: item.value, id: item.id };
        }
      });
    });
  };
  const showTableData = (record: any, column: any) => {
    const { dataIndex } = column;
    const codeAll = Object.keys(record);
    if (codeAll.indexOf(dataIndex) !== -1) {
      return record[codeAll[codeAll.indexOf(dataIndex)]].value;
    } else {
      return '';
    }
  };

  const dataSpanMethod = (datas: any) => {
    const { record, rowIndex, columnIndex, column } = datas;
    const obj = {
      rowspan: 2,
      colspan: 0
    };
    const arr = data.filter((res: any) => {
      return res.grade == record.grade;
    });
    if (
      data[rowIndex - 1]?.grade != record.grade &&
      column.dataIndex === 'grade'
    ) {
      obj.rowspan = arr.length;
    } else {
      obj.rowspan = 0;
    }
    return obj;
  };

  column?.length !== 0 && lineData?.length !== 0 ? getSortData() : null;
  return { columns, data, dataSpanMethod, showTableData };
};
export { matrixData };
